package stark.app.function.smalllan;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.smalllan.lib.SmallLanModule;
import com.stark.smalllan.lib.module.SmallLanDataManager;
import com.stark.smalllan.lib.module.SmallLanDbHelper;
import com.stark.smalllan.lib.module.bean.LanChapter;
import com.stark.smalllan.lib.module.constant.ExerciseType;
import com.stark.smalllan.lib.module.constant.SmallLanType;
import com.stark.smalllan.lib.ui.CommonExerciseActivity;
import com.stark.smalllan.lib.ui.ErrBookActivity;
import com.stark.smalllan.lib.ui.LearnActivity;
import com.stark.smalllan.lib.ui.MyCollectActivity;
import com.stark.smalllan.lib.ui.SelChapterActivity;

import androidx.annotation.NonNull;
import stark.app.R;
import stark.app.databinding.ActivitySmalllanDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.inf.IRetCallback;

public class SmalllanDemoActivity extends BaseNoModelActivity<ActivitySmalllanDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_smalllan_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSelChapter.setOnClickListener(v -> {
            SelChapterActivity.startForRet(this, null);
        });
        mDataBinding.btnMyCollect.setOnClickListener(v -> {
            startActivity(MyCollectActivity.class);
        });
        mDataBinding.btnMyErr.setOnClickListener(v -> {
            startActivity(ErrBookActivity.class);
        });
        mDataBinding.btnLearn.setOnClickListener(v -> {
            LearnActivity.start(this, false, null);
        });
        mDataBinding.btnLookCnWriteSl.setOnClickListener(v -> {
            CommonExerciseActivity.start(this, ExerciseType.LOOK_CN_WRITE_SL, false, null);
        });
        mDataBinding.btnLookCnSelSl.setOnClickListener(v -> {
            CommonExerciseActivity.start(this, ExerciseType.LOOK_CN_SEL_SL, false, null);
        });
        mDataBinding.btnLookSlSelCn.setOnClickListener(v -> {
            CommonExerciseActivity.start(this, ExerciseType.LOOK_SL_SEL_CN, false, null);
        });
        mDataBinding.btnListenSlSelCn.setOnClickListener(v -> {
            CommonExerciseActivity.start(this, ExerciseType.LISTEN_SL_SEL_CN, false, null);
        });
        mDataBinding.btnListenSlSelSl.setOnClickListener(v -> {
            CommonExerciseActivity.start(this, ExerciseType.LISTEN_SL_SEL_SL, false, null);
        });
    }

    @Override
    protected void initData() {
        initSmallLan();
    }

    private void initSmallLan() {
        SmallLanModule.init(SmallLanType.KOREA, "korea.db", new SmallLanModule.InitCallback() {
            @Override
            public void onInit(boolean success) {
                ToastUtils.showShort(success ? "初始化成功" : "初始化失败");
                setListener();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        LanChapter selChapter = SmallLanDataManager.getInstance().getSelChapter();
        if (selChapter != null) {
            SmallLanDbHelper.updateChapter(selChapter, new IRetCallback<LanChapter>() {
                @Override
                public void onResult(LanChapter result) {
                    SmallLanDataManager.getInstance().updateSelChapter(result);
                }
            });
        }
    }

    private void setListener() {
        SmallLanDataManager.getInstance().setListener(new SmallLanDataManager.IListener() {
            @Override
            public void onSelChapterChanged(@NonNull LanChapter chapter) {
                StringBuilder builder = new StringBuilder();
                builder.append("第" + chapter.num + "章: " + chapter.chinese);
                builder.append("\n");
                builder.append("总数：" + chapter.totalCount);
                builder.append("\n");
                builder.append("已学：" + chapter.learnedCount);
                builder.append("\n");
                builder.append("已掌握：" + chapter.graspCount);
                builder.append("\n");
                mDataBinding.tvChapterInfo.setText(builder.toString());
            }
        });
    }
}